<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="会员资料列表"
      row-key="id"
      :indent="20"
      :columns="columns"
      :request-api="getMemberList"
      :data-callback="dataCallback"
    >
      <template #tableHeader>
        <el-button v-auth="'add'" type="primary" :icon="CirclePlus" @click="openDialog('新增', initMemberInfo)">
          新增会员
        </el-button>
      </template>
      <template #balance="scope">
        <div class="flx-justify-between">
          <el-text>{{ scope.row.balance }}</el-text>
          <el-button v-auth="'recharge'" type="primary" link @click="openRechargeDialog(scope.row)"> 充值 </el-button>
        </div>
      </template>
      <template #operation="scope">
        <el-button v-auth="'view'" type="primary" link :icon="EditPen" @click="openDialog('查看', scope.row)"> 查看 </el-button>
        <el-button v-auth="'edit'" type="primary" link :icon="EditPen" @click="openDialog('编辑', scope.row)"> 编辑 </el-button>
        <el-button v-auth="'delete'" type="primary" link :icon="Delete" @click="deleteCategory(scope.row)"> 删除 </el-button>
      </template>
    </ProTable>
    <MemberDialog ref="dialogRef" />
    <RechargeDialog ref="rechargeDialogRef" />
  </div>
</template>

<script setup lang="tsx" name="memberInfo">
import { ref, reactive } from "vue";
import { Member } from "@/api/interface";
import ProTable from "@/components/ProTable/index.vue";
import { ProTableInstance, ColumnProps } from "@/components/ProTable/interface";
import { useHandleData } from "@/hooks/useHandleData";
import { genderType, memberStatus } from "@/utils/serviceDict";
import { getMemberList, editMember, addMember, deleteMember, editUserBalance } from "@/api/modules/member";
import MemberDialog from "@/views/member/components/MemberDialog.vue";
import RechargeDialog from "@/views/member/components/RechargeDialog.vue";
import { CirclePlus, EditPen, Delete } from "@element-plus/icons-vue";

const dataCallback = (data: any) => {
  return {
    list: data.list,
    total: data.total,
    pageNum: data.pageNum,
    pageSize: data.pageSize
  };
};
const proTable = ref<ProTableInstance>();
// 新建会员默认数据
const initMemberInfo = reactive({
  grade: "",
  sex: 1,
  passWord: "",
  status: 1
});

const columns: ColumnProps<Member.ResMemberList>[] = [
  { type: "index", label: "序号", width: 60 },
  { sortable: true, prop: "userName", label: "会员名称", width: 130, align: "left", search: { el: "input" } },
  { sortable: true, prop: "nickName", label: "会员昵称", width: 140, align: "left", search: { el: "input" } },
  { sortable: true, prop: "number", label: "会员卡号", align: "left", width: 130 },
  { sortable: true, prop: "categoryName", label: "会员分类", align: "left", width: 110 },
  { sortable: true, prop: "mobile", label: "手机号", width: 130, align: "left", search: { el: "input" } },
  {
    sortable: true,
    prop: "sex",
    label: "性别",
    // 字典数据
    // enum: genderType,
    // 字典请求不带参数
    enum: genderType,
    align: "left",
    width: 90,
    // 字典请求携带参数
    // enum: () => getUserGender({ id: 1 }),
    search: { el: "select", props: { filterable: true } },
    fieldNames: { label: "label", value: "value" }
  },
  {
    sortable: true,
    prop: "status",
    label: "状态",
    enum: memberStatus,
    width: 90,
    align: "left",
    search: { el: "select", props: { filterable: true } },
    fieldNames: { label: "label", value: "value" }
  },
  {
    sortable: true,
    prop: "gradeName",
    label: "会员等级",
    align: "left",
    width: 130
  },
  { sortable: true, prop: "balance", label: "储值余额", align: "left", width: 150 },
  { sortable: true, prop: "point", label: "积分", align: "left", width: 100 },
  { sortable: true, prop: "starDate", label: "启动日期", align: "left", width: 170 },
  { sortable: true, prop: "endDate", label: "失效日期", align: "left", width: 170 },
  { prop: "operation", label: "操作", width: 220, fixed: "right" }
];

const deleteCategory = async (params: Member.ResMemberList) => {
  await useHandleData(deleteMember, { id: params.id }, `删除【${params.nickName}】会员`);
  proTable.value?.getTableList();
};

const dialogRef = ref<InstanceType<typeof MemberDialog> | null>(null);
const openDialog = (title: string, row: Partial<Member.ResMemberList> = {}) => {
  const params = {
    title,
    row: { ...row },
    isView: title === "查看",
    api: title === "新增" ? addMember : title === "编辑" ? editMember : undefined,
    getTableList: proTable.value?.getTableList
  };
  dialogRef.value?.acceptParams(params);
};
const rechargeDialogRef = ref<InstanceType<typeof RechargeDialog> | null>(null);
const openRechargeDialog = (row: Partial<Member.ResMemberList> = {}) => {
  const params = {
    row: { id: row.id, currentBalance: row.balance, nickName: row.nickName },
    isView: false,
    api: editUserBalance,
    getTableList: proTable.value?.getTableList
  };
  rechargeDialogRef.value?.acceptParams(params);
};
</script>
